package com.itheima.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

    @Configuration
    public class GlobalCorsConfig {
        @Bean
        public CorsFilter corsFilter() {
            // 1. 添加 CORS配置信息
            CorsConfiguration config = new CorsConfiguration();
            // 允许跨域访问的域名，可填写具体域名，*代表允许所有访问
            config.addAllowedOrigin("*");
            // 是否允许携带cookie
            // config.setAllowCredentials(true);
            // 允许访问类型：get  post 等，*代表所有类型
            config.addAllowedMethod("*");
            //放行哪些原始请求头部信息
            config.addAllowedHeader("*");
            //暴露哪些头部信息
            config.addExposedHeader("*");
            // 2. 添加映射路径
            UrlBasedCorsConfigurationSource corsConfigurationSource = new UrlBasedCorsConfigurationSource();
            corsConfigurationSource.registerCorsConfiguration("/**",config);
            // 3. 返回新的CorsFilter
            return new CorsFilter(corsConfigurationSource);
        }
    }

